/*
 * Hibiscus is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * Hibiscus is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Hibiscus; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 * Copyright (C) 2003 Tetete - http://tetete.hu.
 */
package hu.tetete.hibiscus.dal;

import hu.tetete.hibiscus.dal.TaxonDao.TaxonDef;
import hu.tetete.hibiscus.model.Taxon;
import hu.tetete.hibiscus.model.TaxonLevel;
import hu.tetete.hibiscus.util.EnumUtil;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * TODO javadoc.
 */
public class TaxonDatabaseOpenHelper extends SQLiteOpenHelper {

    private static final String TAG = TaxonDatabaseOpenHelper.class.getName();

    private static final int DATABASE_VERSION = 10;

    private static final String DATABASE_NAME = "taxon";

    /**
     * @see SQLiteOpenHelper#SQLiteOpenHelper(Context, String, CursorFactory, int)
     * @param context TODO javadoc.
     */
    public TaxonDatabaseOpenHelper(final Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    /**
     * @see SQLiteOpenHelper#onCreate(SQLiteDatabase)
     */
    @Override
    public void onCreate(final SQLiteDatabase db) {
        /* Generate taxon table */
        TaxonDao.generateDb(db);
        /* Generate property table */
        PropertyDao.generateDb(db);
        /* Generate taxon-property cross table */

        final long rosaceaeId = db.insert("taxon", null, createTaxonContent("Rosaceae", "Rosaceae1", TaxonLevel.FAMILIA));
        final long appleId    = db.insert("taxon", null, createTaxonContent("apple", "apple1", Long.valueOf(rosaceaeId), TaxonLevel.SPECIES));
        final long pearId     = db.insert("taxon", null, createTaxonContent("pear",  "pear2",  Long.valueOf(rosaceaeId), TaxonLevel.SPECIES));

    }

    @Deprecated
    private ContentValues createTaxonContent(final String name, final String scienceName, final TaxonLevel taxonLevel) {
        return createTaxonContent(name, scienceName, taxonLevel.getLevelId());
    }

    @Deprecated
    private ContentValues createTaxonContent(final String name, final String scienceName, final int taxonLevelId) {
        Log.i(TAG, "Added taxon: " + name + " taxonLevelId: " + taxonLevelId);
        final ContentValues insertValues = new ContentValues();
        insertValues.put(TaxonDef.TABLE_COLUMNS.NAME.name(),           name);
        insertValues.put(TaxonDef.TABLE_COLUMNS.SCIENCE_NAME.name(),   scienceName);
        insertValues.put(TaxonDef.TABLE_COLUMNS.TAXON_LEVEL_ID.name(), Integer.valueOf(taxonLevelId));
        return insertValues;
    }

    @Deprecated
    private ContentValues createTaxonContent(final String name, final String scienceName, final Taxon parent, final TaxonLevel taxonLevel) {
        return createTaxonContent(name, scienceName, parent == null ? null : parent.getId(), taxonLevel);
    }

    @Deprecated
    private ContentValues createTaxonContent(final String name, final String scienceName, final Long parentId, final TaxonLevel taxonLevel) {
        final ContentValues insertValues = createTaxonContent(name, scienceName, taxonLevel);
        if (parentId != null) {
            insertValues.put(TaxonDef.TABLE_COLUMNS.PARENT_ID.name(), parentId);
        }
        return insertValues;
    }

    /**
     * @see SQLiteOpenHelper#onUpgrade(SQLiteDatabase, int, int)
     */
    @Override
    public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
        TaxonDao.upgradeDb(db);
        PropertyDao.upgradeDb(db);

        final long rosaceaeId = db.insert("taxon", null, createTaxonContent("Rosaceae", "Rosaceae1", TaxonLevel.FAMILIA));
        final long appleId    = db.insert("taxon", null, createTaxonContent("apple", "apple1", Long.valueOf(rosaceaeId), TaxonLevel.SPECIES));
        final long pearId     = db.insert("taxon", null, createTaxonContent("pear",  "pear2",  Long.valueOf(rosaceaeId), TaxonLevel.SPECIES));

    }

    /**
     * @param id The id of the taxon to retrive its children. Can be null.
     * @return The cursor of result containing the children.
     */
    public Cursor getChildrenOfTaxon(final Long id) {
        Log.i(TAG, "GetChildrenOfTaxon: " + id);

        final String[] columns = EnumUtil.getEnumNames(TaxonDef.TABLE_COLUMNS.class);
        for (final String columnName : columns) {
            Log.i(TAG, "Column: " + columnName);
        }
        final Cursor taxonCursor = getWritableDatabase().query(TaxonDef.TABLE_NAME, columns, TaxonDef.TABLE_COLUMNS.PARENT_ID + (id == null ? " is null" : " = " + id) , null, null, null, TaxonDef.TABLE_COLUMNS.NAME.name());
        if (taxonCursor != null) {
            taxonCursor.moveToFirst();
        }
        return taxonCursor;
    }

    /**
     * TODO javadoc.
     * @param id TODO javadoc.
     * @return TODO javadoc.
     */
    public Cursor getTaxonById(final Long id) {
        Log.i(TAG, "GetTaxonById: " + id);
        final String[] columns = EnumUtil.getEnumNames(TaxonDef.TABLE_COLUMNS.class);
        final Cursor taxonCursor = getReadableDatabase().query(TaxonDef.TABLE_NAME, columns, TaxonDef.TABLE_COLUMNS.ID + "=" + id, null, null, null, null);
        if (taxonCursor != null) {
            taxonCursor.moveToFirst();
        }
        return taxonCursor;
    }

}
